package mju.cn.oneweek;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @program: java-ee_31
 * @description:
 * @author: HP
 * @time: 2020/9/18 16:58
 */
public class JdkProxyExample implements InvocationHandler {
    private Object target= null;
    public Object bind(Object target){
        this.target=target;
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("进入代理逻辑方法");
        System.out.println("在调度真实对象之前的服务");
        Object obj=method.invoke(target,args);
        System.out.println("在调度真实对象之后的服务");
        return obj;
    }
    public void testJdkProxy(){
        JdkProxyExample jdk =new JdkProxyExample();
        HelloWorld proxy=(HelloWorld)jdk.bind(new HelloWorldImpl());
//        HelloWorld proxy=(HelloWorld)JdkProxyExample.bind(new HelloWorldImpl());//需要静态state才能实现
        proxy.sayHelloWorld();
    }

    public static void main(String[] args) {
        new JdkProxyExample().testJdkProxy();
    }
}
